<?php
class GrouppurchaseModel extends Model {
	//写入一个拼团订单
	public function insertOrder($sale_id){
		
		
		
		//找到当前购买的商品
		$sale = M('Sale') -> where(array('id' => $sale_id, 'site_id' => __SITEID__)) -> find();
		if(!M('Grouppurchase') -> where(array('product_id' => $sale['product_id'], 'site_id' => __SITEID__)) -> find()){
			return false;
		}
		
		$product = M('Product') -> where(array('id' => $sale['product_id'], 'site_id' => __SITEID__)) -> find();
		
		if(!$product){
			return false;
		} 
		
		
		$order = M('Order') -> where(array('site_id' => __SITEID__, 'id' => $sale['order_id'])) -> find();
		if($order['aff_id'] == 0){
			$order['aff_id'] = $order['member_id'];
		}
		//当前团的人数
		$grouppurchase_order = M('Grouppurchase_order') -> where(array('product_id' => $sale['product_id'], 'member_id' => $order['member_id'], 'site_id' => __SITEID__)) -> find();
		
		$grouppurchase = M('Grouppurchase') -> where(array('product_id' => $sale['product_id'], 'site_id' => __SITEID__)) -> find();
		$data['order_id'] = $sale['order_id'];
		$data['sale_id'] = $sale['id'];
		$data['site_id'] = __SITEID__; 
		$data['member_id'] = $order['member_id'];
		$data['ordernumber'] = $order['ordernumber'];
		$data['total'] = $sale['total'];
		$data['product_id'] = $sale['product_id']; 
		$data['dateline'] = time();
		$data['grouppurchase_id'] = $grouppurchase['id'];
		if($grouppurchase_order){
			//用户买过了，且没有开团，再买就重新开团
			if(M('Grouppurchase_order') -> where(array('product_id' => $sale['product_id'], 'aff_id' => $order['member_id'], 'site_id' => __SITEID__)) -> find()){
				return false;
			}
			$data['aff_id'] = $order['member_id'];
		}else{
			
			//计算当前团的人数，超过人数后再开团
			$capacity = M('Grouppurchase_order') -> where(array('product_id' => $sale['product_id'], 'aff_id' => $order['aff_id'], 'site_id' => __SITEID__)) -> count();
			if($capacity >= $grouppurchase['capacity']){
				
				
				
				//重新开团
				$data['aff_id'] = $order['member_id'];
			}else{ 
				//入库
				$data['aff_id'] = $order['aff_id'];
			}
		}
		$id = M('Grouppurchase_order') -> add($data);
		
		
		
		
		if($capacity+1 >= $grouppurchase['capacity']){
		//该团设置为组团成功
				$ret = M('Grouppurchase_order') -> data(array('status' => 1)) -> where(array('product_id' => $sale['product_id'], 'aff_id' => $order['aff_id'], 'site_id' => __SITEID__)) -> save();
				//dump($ret); 
				if($ret !== false){
					$msglist = M('Grouppurchase_order') -> alias('g') -> join("left join __MEMBER__ as m on m.id = g.member_id") -> where(array('g.product_id' => $sale['product_id'], 'g.aff_id' => $order['aff_id'], 'g.site_id' => __SITEID__)) -> Field("m.openid,m.nickname,g.*") -> select();
					
					$domain = C('DOMAIN_MAIN');
					$picurl = 'http://'.$domain[0].'/Public'.$product['img_url'];
					
					foreach($msglist as $m){
						$sendWXNews[0]['title'] = '拼团成功通知';
						$sendWXNews[0]['url'] = U('Member/Order/read',array('id' => $m['order_id']),true,false,true);
						$sendWXNews[0]['picurl'] = $picurl;
						$sendWXNews[0]['description'] = '订单号为：'.$m['ordernumber'].'的商品拼团成功！感谢您的参与！';
						//dump($sendWXNews);
						sendWXNews($m['openid'], $sendWXNews);
						sendWXMsg($m['openid'], $grouppurchase['successMsg']);
					}
					
					
				}
		
		
		}else{
			    //$ret = M('Grouppurchase_order') -> data(array('status' => 1)) -> where(array('product_id' => $sale['product_id'], 'aff_id' => $order['aff_id'], 'site_id' => __SITEID__)) -> save();
				//dump($ret); 
				if($ret !== false){
					$msglist = M('Grouppurchase_order') -> alias('g') -> join("left join __MEMBER__ as m on m.id = g.member_id") -> where(array('g.product_id' => $sale['product_id'], 'g.aff_id' => $order['aff_id'], 'g.site_id' => __SITEID__)) -> Field("m.openid,m.nickname,g.*") -> select();
					
					$domain = C('DOMAIN_MAIN');
					$picurl = 'http://'.$domain[0].'/Public'.$product['img_url'];
					
					foreach($msglist as $m){
						sendWXMsg($m['openid'], "您参与的".$grouppurchase['capacity']."人团还要需要".($capacity+1 - $grouppurchase['capacity'])."个人才能拼团成功！快通知朋友来帮忙！");
					}
					
					 
				}
		}
		 
	}
	
	
	public function _list($view,$map,$sort='g.aff_id',$pagesize=20){
		//计算总页数
		$db = M('Grouppurchase_order');
		$count=$db->alias('g') -> join("left join __ORDER__ AS o on g.order_id=o.id") ->where($map) -> count();
		//实例化分页类
	    
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $db->where($map)->alias('g')->join("left join __ORDER__ AS o on g.order_id=o.id")->Field("o.*,g.aff_id,g.sale_id")->order($sort)->limit($p->firstRow.',
'.$p->listRows)->select();
        $saledb = M('Sale');
        foreach($list as $k => $v){
			$sale = $saledb -> where(array('id' => $v['sale_id'])) -> find();
			$list[$k]['pay_status'] = $sale['pay_status'];
		}
        //echo $db -> getLastSql();
        $pages=$p->show();
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
	
}